<?php
class Admin_SchedulingController extends Zend_Controller_Action
{	
    public function init() {
        if (Zend_Auth::getInstance ( 'admin' )->setStorage ( new Zend_Auth_Storage_Session ( 'admin' ) )->hasIdentity () == false) {
			$this->_helper->getHelper ( 'Redirector' )->gotoUrl ( "admin/login" );
		} 
    }
    public function indexAction()
    {
      $this->view->title = "Text link";	
    	try {
            $model = new Vina_Model_DeveloperMapper();
            $dataList = $model->fetchListSchedule("1=1");
            $paginator         = Zend_Paginator::factory($dataList);
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(10);
            $paginator->setPageRange(10);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
           
            $this->view->data = $paginator;     
                
         }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    
    }
    public function addAction() {
        try {
           $model = new Vina_Model_Scheduling();
           $form                   = new Admin_Form_Schedule();
           if ($this->_request->isPost()) {
                $formData = $this->_request->getPost();
                if ($form->isValid($formData)) {
                   $dataInsert = $form->getValues();
                   $timecheck = strtotime($dataInsert['time']);
                   $checkdata = $model->fetchListToArray("time = {$timecheck} and news_id = {$dataInsert['news_id']} and where_leaning = '{$dataInsert['where_leaning']}'");
                   if($checkdata == NULL) {
                   
                   $model->setNewsId($dataInsert['news_id']);
                   $model->setWhereLeaning($dataInsert['where_leaning']);
                   $model->setTime(strtotime($dataInsert['time']));
                   $model->save();
                   }
                   $this->_redirect('/admin/scheduling');
                }
            }
           //set view
           $this->view->form       = $form;
        }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    }
    public function editAction() {
        try {
           $model = new Vina_Model_Scheduling();
           $form                   = new Admin_Form_Schedule();
           $id                     =  $this->_request->getParam('id',false);
           $data = $model->find($id)->toArray();
           $data['time'] = date('Y-m-d', $data['time']);
           $form->populate($data);
            if ($this->_request->isPost()) {
                
                $formData = $this->_request->getPost();

                if ($form->isValid($formData)) {
                   $dataInsert = $form->getValues();
                    $checkdata = $model->fetchListToArray("time = {$dataInsert['time']} and news_id = {$dataInsert['news_id']} and where_leaning = '{$dataInsert['where_leaning']}'");
                   if($checkdata == NULL) {
                   $model->setId($id);
                   $model->setNewsId($dataInsert['news_id']);
                   $model->setWhereLeaning($dataInsert['where_leaning']);
                   $model->setTime(strtotime($dataInsert['time']));
                   $model->save();
                   }
                   $this->_redirect('/admin/scheduling');
                }
            }
           //set view
           $this->view->form       = $form;
        }catch(Exception $ex){
            echo "<pre>";
            print_r($ex->getMessage());
        }
    }
  public function deleteAction() {
     $id                     =  $this->_request->getParam('id',false);
     $model = new Vina_Model_Scheduling();
     $where = "id = $id";
     $model->delete($where);
     $this->_redirect($_SERVER['HTTP_REFERER']);
  }
    	
}
